package com.whcoding.easypoi.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.alibaba.fastjson.JSONObject;
import com.whcoding.easypoi.handler.DeptExcelDataHandler;
import com.whcoding.easypoi.util.LocalJsonUtil;
import com.whcoding.easypoi.vo.OrmDeptExcelVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: spring-boot-learning
 * @description:
 * @author: whcoding
 * @create: 2022-06-28 19:18
 **/
@RequestMapping("/easyPoi")
@RestController
@Api(tags = "EasyPoiController", description = "EasyPoi导入导出测试")
public class EasyPoiController {

	private static final Logger log = LoggerFactory.getLogger(EasyPoiController.class);

	/**
	 * http://localhost:3088/easyPoi/exportDeptList
	 *
	 * @param map
	 * @param request
	 * @param response
	 */
	@ApiOperation(value = "导出部门列表Excel")
	@RequestMapping(value = "/exportDeptList", method = RequestMethod.GET)
	public void exportDeptList(ModelMap map,
							   HttpServletRequest request,
							   HttpServletResponse response) {
		//1.获取对象
		List<OrmDeptExcelVO> deptList = LocalJsonUtil.getListFromJson("json/dept.json", OrmDeptExcelVO.class);
		//2.设置标题sheetName
		ExportParams params = new ExportParams("部门列表", "部门列表", ExcelType.XSSF);
		//3.设置排除字段
//		params.setExclusions(new String[]{"金额", "短名称"});
		//对导出结果进行自定义处理 [也可以剔除]
		DeptExcelDataHandler handler = new DeptExcelDataHandler();
		handler.setNeedHandlerFields(new String[]{"部门类型"});

		params.setDataHandler(handler);
		map.put(NormalExcelConstants.DATA_LIST, deptList);
		map.put(NormalExcelConstants.CLASS, OrmDeptExcelVO.class);
		map.put(NormalExcelConstants.PARAMS, params);
		map.put(NormalExcelConstants.FILE_NAME, "部门信息明细_"+System.currentTimeMillis());
		PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
	}


	/**
	 * 将excel数据导入
	 *
	 * @param file 文件信息
	 */
	@ApiOperation("从Excel导入部门列表")
	@RequestMapping(value = "/importDeptList", method = RequestMethod.POST)
	@ResponseBody
	public void importDeptList(@RequestPart("file") MultipartFile file) {
		ImportParams params = new ImportParams();
		params.setTitleRows(1);
		params.setHeadRows(1);
		try {
			List<OrmDeptExcelVO> list = ExcelImportUtil.importExcel(
					file.getInputStream(),
					OrmDeptExcelVO.class, params);
			System.out.println("dept导入的list明细数据：" + JSONObject.toJSONString(list));
			log.info("导入成功");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("导入失败！");
		}
		log.error("执行完成!");
	}

}
